package com.ruoyi.manage.cms.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.manage.cms.domain.HbZcfg;
import com.ruoyi.manage.cms.domain.vo.HbZcfgVo;
import com.ruoyi.manage.cms.service.IHbZcfgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;

import com.ruoyi.common.utils.ObjUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 政策法规Controller
 *
 * @author ruoyi
 * @date 2025-07-19
 */
@Api(tags="政策法规控制器")
@RestController
@RequestMapping("/manage/zcfg" )
public class HbZcfgController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IHbZcfgService hbZcfgService;

    /**
     * 查询政策法规列表
     */
    @ApiOperation(value = "查询政策法规",notes = "政策法规管理")
    @PreAuthorize("@ss.hasPermi('manage:zcfg:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(HbZcfgVo hbZcfg, QueryWrapper<HbZcfg> queryWrapper) {
        setQueryCondition(hbZcfg,queryWrapper);
        startPage();
        List<HbZcfg> list = hbZcfgService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(HbZcfgVo hbZcfg, QueryWrapper<HbZcfg> queryWrapper) {
        if(StrUtil.isNotBlank(hbZcfg.getTitle())){
            queryWrapper.like(TABLE_PREFIX+"title" ,hbZcfg.getTitle());
        }
        if (ObjUtil.isNotNull(hbZcfg.getBeginPublishTime()) && ObjUtil.isNotNull(hbZcfg.getEndPublishTime())){
            queryWrapper.between(TABLE_PREFIX+"publish_time", DateUtil.beginOfDay(hbZcfg.getBeginPublishTime()),DateUtil.endOfDay(hbZcfg.getEndPublishTime()));
        }
        if(hbZcfg.getStatus() != null){
            queryWrapper.eq(TABLE_PREFIX+"status" ,hbZcfg.getStatus());
        }
        if(hbZcfg.getIsDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"is_deleted" ,hbZcfg.getIsDeleted());
        }
        if(hbZcfg.getIsTop() != null){
            queryWrapper.eq(TABLE_PREFIX+"is_top" ,hbZcfg.getIsTop());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出政策法规列表
     */
    @PreAuthorize("@ss.hasPermi('manage:zcfg:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, HbZcfgVo hbZcfg,QueryWrapper<HbZcfg> queryWrapper) {
        if (CollUtil.isEmpty( hbZcfg.getIds())){
            setQueryCondition(hbZcfg,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",hbZcfg.getIds());
        }
        List<HbZcfg> list = hbZcfgService.list(queryWrapper);
        ExcelUtil<HbZcfg> util = new ExcelUtil<HbZcfg>(HbZcfg. class);
        util.exportExcel(response,list, "zcfg");
    }

    /**
     * 获取政策法规详细信息
     */
    @ApiOperation(value = "获取政策法规",notes = "政策法规管理")
    @PreAuthorize("@ss.hasPermi('manage:zcfg:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(hbZcfgService.queryGetById(id));
    }

    /**
     * 新增政策法规
     */
    @ApiOperation(value = "新增政策法规",notes = "政策法规管理")
    @PreAuthorize("@ss.hasPermi('manage:zcfg:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HbZcfg hbZcfg) {
        return toAjax(hbZcfgService.addHbZcfg(hbZcfg) ? 1 : 0);
    }

    /**
     * 修改政策法规
     */
    @ApiOperation(value = "修改政策法规",notes = "政策法规管理")
    @PreAuthorize("@ss.hasPermi('manage:zcfg:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody HbZcfg hbZcfg) {
        return toAjax(hbZcfgService.updateHbZcfg(hbZcfg) ? 1 : 0);
    }

    /**
     * 删除政策法规
     */
    @ApiOperation(value = "删除政策法规",notes = "政策法规管理")
    @PreAuthorize("@ss.hasPermi('manage:zcfg:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hbZcfgService.deleteHbZcfgByIds(ids) ? 1 : 0);
    }
}
